﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QDebug>


int ifResult = 0;       //  是否有已存结果，1有存储结果/0无记录
int sign = 0;           //  记录运算符号
double num1 = 0;        //  记录数值
double num2 = 0;
double result = 0;      //  记录储存数据
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_2_clicked()                                                                                  //点击"clr"
{
    this->ui->textEdit->clear();        //清空算式框
    this->ui->label->setText(QString::fromLocal8Bit("请输入算式↑"));           //提示输入
}

void MainWindow::on_pushButton_2_pressed()      //点击"clr"时改变颜色
{
    this->ui->pushButton_2->setStyleSheet("QPushButton{color:green;border:1px solid black;"
                                          "border-radius:16px;background-color: rgba(238, 255, 255, 150);}");
}

void MainWindow::on_pushButton_2_released()
{
    this->ui->pushButton_2->setStyleSheet("QPushButton{color:black;border:1px solid black;"
                                          "border-radius:16px;background-color: rgba(238, 255, 255, 150);}");
}

void MainWindow::on_pushButton_pressed()        //点击"="时改变颜色
{
    this->ui->pushButton->setStyleSheet("QPushButton{color:green;border:1px solid black;"
                                          "border-radius:16px;background-color: rgba(238, 255, 255, 150);}");
}


void MainWindow::on_pushButton_released()
{
    this->ui->pushButton->setStyleSheet("QPushButton{color:black;border:1px solid black;"
                                          "border-radius:16px;background-color: rgba(238, 255, 255, 150);}");
}


void MainWindow::on_pushButton_3_clicked()                                                                                  //点击"+"
{
    sign = 1;       //记录符号+
    if(ifResult==0 && (this->ui->textEdit->document()->isEmpty()==0)){
        QString con1=this->ui->textEdit->toPlainText();     //--无储存结果时使用输入值作为num1
        if(con1.contains(QRegExp("^[0-9/./-]*$"))==0){
            this->ui->label->setText(QString::fromLocal8Bit("请规范输入：输入纯数值!!"));      //输入非纯数值，则清空输入框，显示提示
            this->ui->textEdit->clear();
        }
        else {
            num1=con1.toDouble();
            this->ui->textEdit->setText(QString::number(num1)+"+");
        }
    }
    else if(ifResult==1 && (this->ui->textEdit->document()->isEmpty())==1){
        num1=result;                                        //--有储存结果无输入则直接使用result作为num1
        this->ui->textEdit->setText(QString::number(num1)+"+");                 //显示算式
    }
    else if(ifResult==1 && (this->ui->textEdit->document()->isEmpty())==0){
        QString con1=this->ui->textEdit->toPlainText();     //--有储存结果有输入则使用输入值作为num1
        if(con1.contains(QRegExp("^[0-9/./-]*$"))==0){
            this->ui->label->setText(QString::fromLocal8Bit("请规范输入：输入纯数值!!"));
            this->ui->textEdit->clear();
        }
        else {
            num1=con1.toDouble();
            this->ui->textEdit->setText(QString::number(num1)+"+");
        }
    }
    else {
        this->ui->label->setText(QString::fromLocal8Bit("请输入数值!!"));//--无输入时显示提示
    }

}

void MainWindow::on_pushButton_4_clicked()                                                                                  //点击"-"
{
    sign = 2;       //记录符号-
    if(ifResult==0 && (this->ui->textEdit->document()->isEmpty())==0){
        QString con1=this->ui->textEdit->toPlainText();
        if(con1.contains(QRegExp("^[0-9/./-]*$"))==0){
            this->ui->label->setText(QString::fromLocal8Bit("请规范输入：输入纯数值!!"));
            this->ui->textEdit->clear();
        }
        else {
            num1=con1.toDouble();
            this->ui->textEdit->setText(QString::number(num1)+"-");
        }
    }
    else if(ifResult==1 && (this->ui->textEdit->document()->isEmpty())==1){
        num1=result;
        this->ui->textEdit->setText(QString::number(num1)+"+");
    }
    else if(ifResult==1 && (this->ui->textEdit->document()->isEmpty())==0){
        QString con1=this->ui->textEdit->toPlainText();
        if(con1.contains(QRegExp("^[0-9/./-]*$"))==0){
            this->ui->label->setText(QString::fromLocal8Bit("请规范输入：输入纯数值!!"));
            this->ui->textEdit->clear();
        }
        else {
            num1=con1.toDouble();
            this->ui->textEdit->setText(QString::number(num1)+"-");
        }
    }
    else {
        this->ui->label->setText(QString::fromLocal8Bit("请输入数值!!"));
    }
}

void MainWindow::on_pushButton_5_clicked()                                                                                  //点击"×"
{
    sign = 3;       //记录符号×
    if(ifResult==0 && (this->ui->textEdit->document()->isEmpty())==0){
        QString con1=this->ui->textEdit->toPlainText();
        if(con1.contains(QRegExp("^[0-9/./-]*$"))==0){
            this->ui->label->setText(QString::fromLocal8Bit("请规范输入：输入纯数值!!"));
            this->ui->textEdit->clear();
        }
        else{num1=con1.toDouble();
        this->ui->textEdit->setText(QString::number(num1)+QString::fromLocal8Bit("×"));
        }
    }
    else if(ifResult==1 && (this->ui->textEdit->document()->isEmpty())==1){
        num1=result;
        this->ui->textEdit->setText(QString::number(num1)+QString::fromLocal8Bit("×"));
    }
    else if(ifResult==1 && (this->ui->textEdit->document()->isEmpty())==0){
        QString con1=this->ui->textEdit->toPlainText();
        if(con1.contains(QRegExp("^[0-9/./-]*$"))==0){
            this->ui->label->setText(QString::fromLocal8Bit("请规范输入：输入纯数值!!"));
            this->ui->textEdit->clear();
        }
        else{num1=con1.toDouble();
        this->ui->textEdit->setText(QString::number(num1)+QString::fromLocal8Bit("×"));
        }
    }
    else {
        this->ui->label->setText(QString::fromLocal8Bit("请输入数值!!"));
    }
}

void MainWindow::on_pushButton_6_clicked()                                                                                  //点击"÷"
{
    sign = 4;       //记录符号÷
    if(ifResult==0 && (this->ui->textEdit->document()->isEmpty())==0){
        QString con1=this->ui->textEdit->toPlainText();
        if(con1.contains(QRegExp("^[0-9/./-]*$"))==0){
            this->ui->label->setText(QString::fromLocal8Bit("请规范输入：输入纯数值!!"));
            this->ui->textEdit->clear();
        }
        else{num1=con1.toDouble();
        this->ui->textEdit->setText(QString::number(num1)+QString::fromLocal8Bit("÷"));
        }
    }
    else if(ifResult==1 && (this->ui->textEdit->document()->isEmpty())==1){
        num1=result;
        this->ui->textEdit->setText(QString::number(num1)+QString::fromLocal8Bit("÷"));
    }
    else if(ifResult==1 && (this->ui->textEdit->document()->isEmpty())==0){
        QString con1=this->ui->textEdit->toPlainText();
        if(con1.contains(QRegExp("^[0-9/./-]*$"))==0){
            this->ui->label->setText(QString::fromLocal8Bit("请规范输入：输入纯数值!!"));
            this->ui->textEdit->clear();
        }
        else{num1=con1.toDouble();
        this->ui->textEdit->setText(QString::number(num1)+QString::fromLocal8Bit("÷"));
        }
    }
    else {
        this->ui->label->setText(QString::fromLocal8Bit("请输入数值!!"));
    }
}

void MainWindow::on_pushButton_clicked()                                                                                  //点击"="
{
    this->ui->label->clear();
    QString expression=this->ui->textEdit->toPlainText();//读算式
    int i;
    QString con2;
    switch (sign) {
        case 0:             //*未输入运算符号
            this->ui->label->setText(QString::fromLocal8Bit("算式不完整，请重新输入！！"));
            this->ui->textEdit->clear();
            break;
    case 1:                 //*加法运算
        i=expression.indexOf("+");
        con2=expression.remove(0,i+1);      //获取num2
        if(con2==""){                       //未输入num2,清除算式，显示提示
            this->ui->label->setText(QString::fromLocal8Bit("算式不完整，请重新输入！！"));
            this->ui->textEdit->clear();
            goto outloop;
        }
        num2=con2.toDouble();
        result=num1+num2;
        ifResult=1;
        break;
    case 2:                 //*减法运算
        i=expression.indexOf("-");
        con2=expression.remove(0,i+1);
        if(con2==""){
            this->ui->label->setText(QString::fromLocal8Bit("算式不完整，请重新输入！！"));
            this->ui->textEdit->clear();
            goto outloop;
        }
        num2=con2.toDouble();
        result=num1-num2;
        ifResult=1;
        break;
    case 3:                 //*乘法运算
        i=expression.indexOf(QString::fromLocal8Bit("×"));
        con2=expression.remove(0,i+1);
        if(con2==""){
            this->ui->label->setText(QString::fromLocal8Bit("算式不完整，请重新输入！！"));
            this->ui->textEdit->clear();
            goto outloop;
        }
        num2=con2.toDouble();
        result=num1*num2;
        ifResult=1;
        break;
    case 4:                 //*除法运算
        i=expression.indexOf(QString::fromLocal8Bit("÷"));
        con2=expression.remove(0,i+1);
        if(con2==""){
            this->ui->label->setText(QString::fromLocal8Bit("算式不完整，请重新输入！！"));
            this->ui->textEdit->clear();
            goto outloop;
        }
        num2=con2.toDouble();
        result=num1/num2;
        ifResult=1;
        break;
    }
    this->ui->textEdit->setText(QString::number(result));       //显示结果
    outloop:
    sign=0;
}
